//
//  XLOrderTableViewCell.m
//  XiangLove
//
//  Created by 陈宪栋 on 2018/1/25.
//  Copyright © 2018年 BAOMIHUA.COM. All rights reserved.
//

#import "XLOrderTableViewCell.h"

@implementation XLOrderTableViewCell

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        self.selectionStyle = UITableViewCellSelectionStyleNone;
        self.contentView.backgroundColor = XLThemeColor;
        self.backgroundColor = [UIColor clearColor];
        
        _avatarView = [[UIImageView alloc] init];
        _avatarView.layer.cornerRadius = 24;
        _avatarView.layer.masksToBounds = YES;
        [self.contentView addSubview:_avatarView];
        [_avatarView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerY.equalTo(self.contentView);
            make.left.equalTo(self.contentView.mas_left).offset(15);
            make.size.mas_equalTo(CGSizeMake(48, 48));
        }];
        
        _nameLabel = [[UILabel alloc] init];
        _nameLabel.font = [UIFont systemFontOfSize:16];
        _nameLabel.textColor = [UIColor whiteColor];
        [self.contentView addSubview:_nameLabel];
        [_nameLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(_avatarView.mas_right).offset(10);
            make.centerY.equalTo(self.contentView);
        }];
        
        _button = [[UIButton alloc] init];
        _button.layer.cornerRadius = 14;
        _button.layer.masksToBounds = YES;
        _button.titleLabel.font = [UIFont systemFontOfSize:14];
        [_button setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        [_button setBackgroundImage:[self buttonImageFromColors:@[HEXCOLOR(0xE55561), HEXCOLOR(0xE5AF65)] frame:CGRectMake(0, 0, 74, 28)] forState:UIControlStateNormal];
        [_button setBackgroundImage:[self buttonImageFromColors:@[HEXCOLOR(0xE55561), HEXCOLOR(0xE5AF65)] frame:CGRectMake(0, 0, 74, 28)] forState:UIControlStateHighlighted];
        [_button setTitle:@"抢接" forState:UIControlStateNormal];
        [_button addTarget:self action:@selector(grabAction) forControlEvents:UIControlEventTouchUpInside];
        [self.contentView addSubview:_button];
        [_button mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerY.equalTo(self.contentView);
            make.right.equalTo(self.contentView.mas_right).offset(-15);
            make.size.mas_equalTo(CGSizeMake(74, 28));
        }];
    }
    return self;
}

- (void)grabAction {
    if (self.grabBlock) {
        self.grabBlock();
    }
}

- (UIImage *)buttonImageFromColors:(NSArray*)colors frame:(CGRect)frame {
    NSMutableArray *ar = [NSMutableArray array];
    for(UIColor *c in colors) {
        [ar addObject:(id)c.CGColor];
    }
    UIGraphicsBeginImageContextWithOptions(frame.size, YES, 1);
    CGContextRef context = UIGraphicsGetCurrentContext();
    CGContextSaveGState(context);
    CGColorSpaceRef colorSpace = CGColorGetColorSpace([[colors lastObject] CGColor]);
    CGGradientRef gradient = CGGradientCreateWithColors(colorSpace, (CFArrayRef)ar, NULL);
    CGPoint start;
    CGPoint end;
    start = CGPointMake(0.0, 0.0);
    end = CGPointMake(frame.size.width, 0.0);
    CGContextDrawLinearGradient(context, gradient, start, end, kCGGradientDrawsBeforeStartLocation | kCGGradientDrawsAfterEndLocation);
    UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
    CGGradientRelease(gradient);
    CGContextRestoreGState(context);
    CGColorSpaceRelease(colorSpace);
    UIGraphicsEndImageContext();
    return image;
}

@end
